package com.yellow.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yellow.dao.MemberDao;
import com.yellow.dao.OrderDao;
import com.yellow.dao.SetMealDao;
import com.yellow.pojo.Member;
import com.yellow.pojo.Setmeal;
import com.yellow.service.MemberService;
import com.yellow.utils.DateUtils;
import com.yellow.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.util.*;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private SetMealDao setMealDao;

    @Override
    public Member findMemberByTelNum(String telephone) {
        return memberDao.findMemberByTelNum(telephone);
    }

    @Override
    public void add(Member add_member) {
        //如果是通过表单，注册用户，也就存在密码
        String password = add_member.getPassword();
        if (password != null) {
            password = MD5Utils.md5(password);
            add_member.setPassword(password);
        }
        memberDao.add(add_member);
    }

    @Override
    public Map<String, Object> getMemberReport() {
        Map<String, Object> map = new HashMap<>();
        List<Date> dates = memberDao.findMemberRegMonths();
        List<Integer> counts = memberDao.findMemberMonthCounts();
        map.put("months", dates);
        map.put("memberCount", counts);
        return map;
    }

    @Override
    public List<Integer> getMemberByMonths(List<String> list) {
        List<Integer> counts = new ArrayList<>();
        for (String month : list) {
            counts.add(memberDao.findMemberCountByMonths(month));
        }
        return counts;
    }

    @Override
    public List<String> findAllSetMealName() {
        return memberDao.findAllSetMealName();
    }

    @Override
    public List<Map<String, String>> findAllSetMealCounts(List<String> setmeals) {
        List<Map<String, String>> list = new ArrayList<>();
        for (String setmeal : setmeals) {
            Map<String, String> map = new HashMap<>();
            map.put("value", memberDao.findsetMealCountBysetMealName(setmeal));
            map.put("name", setmeal);
            list.add(map);
        }
        return list;
    }

    @Override
    public Map getBusinessReportData() throws Exception {
        //查询日期reportDate
        java.util.Date _date = new java.util.Date();
        java.util.Date thisWeekMonday = DateUtils.getThisWeekMonday();
        java.util.Date sundayOfThisWeek = DateUtils.getSundayOfThisWeek();
        String weekMonday = DateUtils.parseDate2String(thisWeekMonday);
        String sunday = DateUtils.parseDate2String(sundayOfThisWeek);
        Map weekbeg_end = new HashMap();
        weekbeg_end.put("monday", weekMonday);
        weekbeg_end.put("sunday", sunday);
        //reportDate
        String date = DateUtils.parseDate2String(_date, "yyyy-MM-dd");
        //获取todayNewMember
        int new_member = memberDao.findtodayNewMember(date);
        //totalMember
        int totalMember = memberDao.totalMember();
        //thisWeekNewMember
        int thisWeekNewMember = memberDao.findthisWeekNewMember(weekbeg_end);
        //thisMonthNewMember
        //获取当前的月份
        String month = DateUtils.parseDate2String(new java.util.Date(), "yyyy-MM");
        int thisMonthNewMember = memberDao.findthisMonthNewMember(month);
        //todayOrderNumber
        int todayOrderNumber = orderDao.findtodayOrderNumber(date);
        //todayVisitsNumber
        int todayVisitsNumber = orderDao.findtodayVisitsNumber(date);
        //thisWeekOrderNumber
        int thisWeekOrderNumber = orderDao.findthisWeekOrderNumber(weekbeg_end);
        //thisWeekVisitsNumber
        int thisWeekVisitsNumber = orderDao.findthisWeekVisitsNumber(weekbeg_end);
        //thisMonthOrderNumber
        int thisMonthOrderNumber = orderDao.findthisMonthOrderNumber(month);
        //thisMonthVisitsNumber
        int thisMonthVisitsNumber = orderDao.findthisMonthVisitsNumber();
        //hotSetmeal 获取前两个热门的套餐
        //获取前两个套餐的id
        //获取总预约套餐的数量
        int totalCount = orderDao.findtotalOrdercount();
        //获取对应套餐id的名称，和数量，占比
        List<Map> hotSetmeal = setMealDao.findHotSetmealNameAndCount(totalCount);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("reportDate", date);
        map.put("todayNewMember", new_member);
        map.put("totalMember", totalMember);
        map.put("thisWeekNewMember", thisWeekNewMember);
        map.put("thisMonthNewMember", thisMonthNewMember);
        map.put("todayOrderNumber", todayOrderNumber);
        map.put("todayVisitsNumber", todayVisitsNumber);
        map.put("thisWeekOrderNumber", thisWeekOrderNumber);
        map.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
        map.put("thisMonthOrderNumber", thisMonthOrderNumber);
        map.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
        map.put("hotSetmeal", hotSetmeal);
        return map;
    }

}
